//
//  LTQPhotoView.swift
//  WeiBoProject
//
//  Created by MAC on 16/3/7.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit

let LTQPhotoViewMargin: CGFloat = 10
let LTQPhotoItmeMargin: CGFloat = 5
let LTQPhotoViewReuseIdentifier = "LTQPhotoViewReuseIdentifier"

protocol LTQPhotoCellDelegate: NSObjectProtocol{
    func composePhotoCellRemove(indexPath:NSIndexPath)
}

class LTQPhotoView: UICollectionView,UICollectionViewDelegate,UICollectionViewDataSource,LTQPhotoCellDelegate {

//    定义一个私有的数组用来存放图片
    var images:[UIImage] = [UIImage]()
    
    //    定义闭包
    var selectPictureBlock:(()->())?

    var flowLayout = UICollectionViewFlowLayout()
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        
        registerClass(LTQPhotoCell.self, forCellWithReuseIdentifier: LTQPhotoViewReuseIdentifier)
        
        delegate = self
        dataSource = self
        
        let ItemHW = (kUIScreenWidth - 2*(LTQPhotoViewMargin + LTQPhotoItmeMargin))/3
        flowLayout.itemSize = CGSizeMake(ItemHW, ItemHW)
        flowLayout.minimumInteritemSpacing = LTQPhotoItmeMargin
        flowLayout.minimumLineSpacing = LTQPhotoItmeMargin
    }
    
    
    
//    MARK:--对外公开一个方法,用于添加图片
    func addImage(image:UIImage){
        
        images.append(image)
        
//        需要刷新数据
        reloadData()
    }

}
extension LTQPhotoView {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = images.count
        return count == 0 || count == 9 ? count : count+1
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LTQPhotoViewReuseIdentifier, forIndexPath:indexPath) as! LTQPhotoCell

        cell.delegate = self
        cell.indexPath = indexPath
//        如果当前row+1超出count范围, 不取图片
        cell.image = indexPath.item + 1 > images.count ? nil : images[indexPath.item]
        
//        cell.image = images[indexPath.item]
        
        return cell
        
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
//        取消选中
        collectionView.deselectItemAtIndexPath(indexPath, animated: false)
        
        if indexPath.item == images.count{
            
            print("添加图片")
//            闭包进行回调
        
            selectPictureBlock!()
        }
        
    }
    func composePhotoCellRemove(indexPath: NSIndexPath) {
        self.images.removeAtIndex(indexPath.row)
        
        reloadData()
    }
    
}

class LTQPhotoCell: UICollectionViewCell {
    
//    设置代理对象
    weak var delegate: LTQPhotoCellDelegate?
    
    var image: UIImage?{
        didSet{
            if image == nil{
                iconImageView.image = UIImage(named: "compose_pic_add")
//                
               deleteButton.hidden = true
                
            }else{
                iconImageView.image = image
                deleteButton.hidden = false
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        
        addSubview(iconImageView)
        addSubview(deleteButton)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
        }
    }
    var indexPath: NSIndexPath?
    
    func click(){
        
        delegate?.composePhotoCellRemove(indexPath!)
        
    }
    
    private lazy var iconImageView:UIImageView = UIImageView()
    
    private lazy var deleteButton: UIButton = {
       
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_card_delete_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        btn.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        
    }()
    
    
}